package com.regedit.aplicacion.activities;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.regedit.aplicacion.R;
import com.regedit.aplicacion.adapters.MiSpinnerAdapter;
import com.regedit.aplicacion.controllers.CarroController;
import com.regedit.aplicacion.dao.EntityDao;
import com.regedit.aplicacion.models.Carro;

public class SpinnerActivity extends Activity {

	private Context mContext;
	private Spinner spnCarros;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_spinner);
		mContext = this;

		fillSpiner();
	}

	private void fillSpiner() {
		CarroController cController = new CarroController(mContext);
		final Cursor cursor = cController
				.obtenerCarrosConContentProviderMix("");
		final String[] arrFrom = new String[] { EntityDao.COL_TITLE };
		final int[] arrTo = new int[] { R.id.marca };
		final MiSpinnerAdapter adap = new MiSpinnerAdapter(mContext,
				R.layout.spinner_item, cursor, arrFrom, arrTo);

		adap.setDropDownViewResource(R.layout.spinner_dropdown_item);

		spnCarros = (Spinner) findViewById(R.id.spinner_carros);
		spnCarros.setAdapter(adap);

		spnCarros.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				Carro carro = (Carro) spnCarros.getSelectedItem();
				TextView tview = (TextView) findViewById(R.id.carroText);
				if (carro.getId() == -1) {
					tview.setText("No ha seleccionado nada");
				} else {
					tview.setText(carro.getMarca());
				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				Toast.makeText(mContext, "no seleccione nada",
						Toast.LENGTH_SHORT).show();

			}
		});
	}

}
